﻿using Oracle.ManagedDataAccess.Client;
using Server.Constant;
using Server.Enum;
using Server.Framework.Message;
using Server.Framework.Processor;
using Server.Framework.Service;
using Server.Framework.Socket;
using Server.Message.Request;
using Server.Message.Response;
using Server.Model;
using Server.OptExp;
using Server.Util;
using System;
using System.Data.Common;

namespace Server.Processor
{
    /// <summary>
    /// Author: Leil
    /// Description: 请求权限列表
    /// CreateTime: 2020/5/13 10:12:49
    /// </summary>
    [ProcessorAttr(MessageId.REQUEST_ALLOT_PERMISSION)]
    class RequestAllotPermissionProcessor : IProcessor
    {

        public void Process(ProtobufAppSession session, RequestBaseMessage message)
        {
            if (message is AllotPerReq msg)
            {

                DbConnection connection = DBService.Instance().GetConnection();
                try
                {
                    connection.Open();
                    using (DbCommand command = connection.CreateCommand())
                    {
                        string delSql = $"DELETE FROM SYS_ROLE_MENU WHERE ROLE_ID = {msg.RoleId}";
                        command.CommandText = delSql;
                        command.ExecuteNonQuery();
                        msg.AllotMenuList.ForEach(allotMenu =>
                        {
                            string insertSql = $"INSERT INTO SYS_ROLE_MENU (ROLE_ID, MENU_ID, CHECKED_TYPE) VALUES ({msg.RoleId}, {allotMenu.MenuId}, {allotMenu.CheckedType})";
                            command.CommandText = insertSql;
                            command.ExecuteNonQuery();
                        });
                    }
                    connection.Close();
                }
                catch (Exception e)
                {
                    throw new CommonOptException(e.Message);
                }



            }
        }





    }
}
